<?php

require_once("BaseDAO.php");
require_once("../Entities/Note.php");

class NoteDAO extends BaseDAO
{
	function CreateNote($item)
	{
		$sql = "insert into Notes(Content ,CreatedBy, CreationOn) values ('" . 
			$item->Content . "','" . $item->CreatedBy . "',NOW());";
		mysql_query($sql);
		$item->Id = mysql_insert_id();
		return $item;
	}
	
	function UpdateNote($item)
	{
		$sql = "update Notes set Content='" . $item->Content . "' where Id='" . $item->Id . "';";
		mysql_query($sql);
	}
	
	function DeleteNote($id)
	{
		$sql = "delete from Notes where Id='" . $id . "';";
		mysql_query($sql);
	}
	
	function GetNoteById($id)
	{
		$sql = "select * from Notes where Id='" . $id . "';";
		$result = mysql_query($sql);
		if(mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_assoc($result);
			$item = new Note();
			$item->Id = $row["Id"];
			$item->Content = $row["Content"];
			$item->CreatedBy = $row["CreatedBy"];
			$item->CreationOn = $row["CreationOn"];
			return $item;
		}
		return null;
	}
	
	function GetNotesByUser($userId)
	{
		$sql = "select * from Notes where CreatedBy='" . $userId . "';";
		$result = mysql_query($sql);
		$kq = array();
		while($row = mysql_fetch_assoc($result))
		{
			$item = new Note();
			$item->Id = $row["Id"];
			$item->Content = $row["Content"];
			$item->CreatedBy = $row["CreatedBy"];
			$item->CreationOn = $row["CreationOn"];
			$kq[] = $item;
		}
		return $kq;
	}
}

?>